/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.stylefeng.guns.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.constant.dictmap.DeleteDict;
import cn.stylefeng.guns.core.common.constant.dictmap.DeptDict;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.node.TreeviewNode;
import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.log.LogObjectHolder;

import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.system.entity.Customer;
import cn.stylefeng.guns.modular.system.entity.Dept;
import cn.stylefeng.guns.modular.system.entity.FruitProcurement;
import cn.stylefeng.guns.modular.system.model.DeptDto;
import cn.stylefeng.guns.modular.system.service.DeptService;
import cn.stylefeng.guns.modular.system.service.FruitProcurementService;
import cn.stylefeng.guns.modular.system.warpper.DeptTreeWrapper;
import cn.stylefeng.guns.modular.system.warpper.DeptWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.treebuild.DefaultTreeBuildFactory;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 水果采购控制器
 *
 * @author zyx
 * @Date  2019-06-24
 */
@Controller
@RequestMapping("/fruit_procurement")
public class FruitProcurementController extends BaseController {

    private String PREFIX = "/modular/system/procurement/";

    @Autowired
    private FruitProcurementService fruitProcurementService;


    /**
     * 跳转到采购首页
     *
     * @author zyx
     * @Date 2019/6/25
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "procurement.html";
    }
    /**
     * 跳转到添加通知
     *
     * @author zyx
     * @Date 2019/06/24
     */
    @RequestMapping("/procurement_add")
    public String noticeAdd() {
        return PREFIX + "procurement_add.html";
    }

    /**
     * 跳转到采购
     *
     * @author zyx
     * @Date 2019/6/25
     */
    @Permission
    @RequestMapping("/update")
    public String procurementUpdate(@RequestParam("fruitProcurementId") Long fruitProcurementId, Model model) {
        FruitProcurement fruitProcurement = this.fruitProcurementService.getById(fruitProcurementId);
        model.addAttribute("fruitProcurement",fruitProcurement);
        LogObjectHolder.me().set(fruitProcurement);
        return PREFIX + "procurement_edit.html";

    }
    /**
     * 跳转到修改客户
     *
     * @author zxs
     * @Date 2018/6/25 4:56 PM
     */
    @Permission
    @RequestMapping("/customer_detail")
    public String customerDetail(@RequestParam("fruitProcurementId") Long fruitProcurementId, Model model) {
        FruitProcurement fruitProcurement = this.fruitProcurementService.getById(fruitProcurementId);
        model.addAttribute("customer",fruitProcurement);
        LogObjectHolder.me().set(fruitProcurement);
        return PREFIX + "customer_detail.html";
    }
    /**
     * 获取所有采购列表
     *
     * @author zyx
     * @Date 2019/6/25
     */
    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(value = "name", required = false) String name,
                       @RequestParam(value = "address", required = false) String address) {
        Page<Map<String, Object>> list = this.fruitProcurementService.list(name, address);
        Page<Map<String, Object>> wrap = new DeptWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 新增采购
     *
     * @author zyx
     * @Date 2019/6/25
     */

    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public ResponseData add(FruitProcurement fruitProcurement) {
        if (ToolUtil.isOneEmpty(fruitProcurement)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        fruitProcurement.setPersonnel(ShiroKit.getUserNotNull().getId());
        System.out.println("********************"+ShiroKit.getUserNotNull().getId());
        fruitProcurement.setTime(new Date());
        this.fruitProcurementService.save(fruitProcurement);
        return ResponseData.success();
    }

    /**
     * 删除信息
     *
     * @author
     * @Date 2019-06-25
     */
    @RequestMapping("/delete")
    @ResponseBody
    @BussinessLog(value = "删除通知", key = "fruitProcurementId", dict = DeleteDict.class)
    public ResponseData delete(@RequestParam Long fruitProcurementId) {
        //缓存通知名称
        LogObjectHolder.me().set(ConstantFactory.me().getNoticeTitle(fruitProcurementId));

        this.fruitProcurementService.removeById(fruitProcurementId);
        return ResponseData.success();
    }
    /**
     * 修改信息
     *
     * @author zyx
     * @Date 22019/06/24
     */
    @RequestMapping(value = "/edit")
    @Permission
    @ResponseBody
    public ResponseData update(FruitProcurement fruitProcurement) {
        System.out.println("dddddddddddddddddddddd"+fruitProcurement.getId());
        this.fruitProcurementService.updateById(fruitProcurement);
        return ResponseData.success();
    }

}
